package com.huanzhu.asuper.supervisionapp.cjbjdudao.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.huanzhu.asuper.supervisionapp.cjbjdudao.R;
import com.huanzhu.asuper.supervisionapp.cjbjdudao.uitls.AppApplicationContext;


/**
 * Created by Ruanpeng on 2018/3/20.
 */

public class BottomDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private int layoutResID;
    private TextView pick_photo_album, pick_photo_camera, pick_photo_cancel;

    /**
     * 要监听的控件id
     */
    private int[] listenedItems;

    private OnBottomMenuItemClickListener listener;


    public BottomDialog(Context context) {
        super(context, R.style.dialog_custom);
        this.context = context;
        this.layoutResID = R.layout.dialog_bottom_layout;
        this.listenedItems = new int[]{R.id.pick_photo_album, R.id.pick_photo_camera, R.id.pick_photo_cancel};
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画
        View view = LayoutInflater.from(   AppApplicationContext.context).inflate(R.layout.dialog_bottom_layout, null);

        setContentView(view);
        pick_photo_album = view.findViewById(R.id.pick_photo_album);
        pick_photo_camera = view.findViewById(R.id.pick_photo_camera);
        pick_photo_cancel = view.findViewById(R.id.pick_photo_cancel);

        //宽度全屏
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() * 9 / 10; // 设置宽度
        getWindow().setAttributes(lp);
        //点击Dialog外部消失
        setCanceledOnTouchOutside(true);
        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }


    }


    public interface OnBottomMenuItemClickListener {

        void onBottomMenuItemClick(BottomDialog dialog, int code);

    }

    public void setOnBottomMenuItemClickListener(OnBottomMenuItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (v == pick_photo_album) {
            if (listener!=null){
                listener.onBottomMenuItemClick(this, 1);
            }

        } else if (v == pick_photo_camera) {
            if (listener!=null){
                listener.onBottomMenuItemClick(this, 2);
            }
        } else if (v == pick_photo_cancel) {
            if (listener!=null){
                listener.onBottomMenuItemClick(this, 3);
            }


        }
        dismiss();

    }
}
